<?php
class Ajax_upload extends Controller{
    function Ajax_upload(){
        
        parent::Controller();
    }   
    
    function upload_directory_gif(){
      $this->load->library('GifCreator');
     // $param_array = $this->uri->uri_to_assoc(1);
     // echo json_encode($param_array);
      $name = $this->input->post('name');
       $image1 = $this->input->post('image1');
       $image2 = $this->input->post('image2');
       $image3 = $this->input->post('image3');
       $image4 = $this->input->post('image4');
              // Instanciate the class (uses default options with the addition of width/height specified)
       $gif = new GifCreator(0, 2, array(-1, -1, -1), 72, 72);
        
        // Add each frame to the animation
        $gif->addFrame(file_get_contents(site_url('uploads').'/directory/userprofile/crop/'.$image1), 200, true);
        $gif->addFrame(file_get_contents(site_url('uploads').'/directory/userprofile/crop/'.$image2), 200, true);
        $gif->addFrame(file_get_contents(site_url('uploads').'/directory/userprofile/crop/'.$image3), 200, true);
        $gif->addFrame(file_get_contents(site_url('uploads').'/directory/userprofile/crop/'.$image4), 200, true);
        // Disposal set to 0 for this frame so that following frame becomes overlay
       // $gif->addFrame(file_get_contents('images/5.jpg'), 200, true, 0, 0, 0);
        // Overlay frames
       // $gif->addFrame(file_get_contents('images/6.gif'), 200, false, 150, 150, 2, array(255, 255, 255));
       $ran_name = 'profile-'.$name.'-'.rand().'.gif';
        
        $fp = fopen('uploads/directory/company_logo/thumb/'.$ran_name, 'w');
        fwrite($fp, $gif->getAnimation());
        fclose($fp);
         echo $ran_name;      
     /*   //echo  'test';
        
// Instanciate the class (uses default options with the addition of width/height specified)


// Add each frame to the animation
$gif->addFrame(file_get_contents(site_url('uploads').'/images/1.jpg'), 200, true);
$gif->addFrame(file_get_contents(site_url('uploads').'/images/2.jpg'), 200, true);
$gif->addFrame(file_get_contents(site_url('uploads').'/images/3.jpg'), 200, true);
$gif->addFrame(file_get_contents(site_url('uploads').'/images/4.jpg'), 200, true);
// Disposal set to 0 for this frame so that following frame becomes overlay
$gif->addFrame(file_get_contents(site_url('uploads').'/images/5.jpg'), 200, true, 0, 0, 0);
// Overlay frame
$gif->addFrame(file_get_contents(site_url('uploads').'/images/6.gif'), 200, false, 150, 150, 2, array(255, 255, 255));
$ran_name = 'profile-'.rand().'.gif';
$fp = fopen('uploads/directory/photo/thumb/'.$ran_name, 'w');
fwrite($fp, $gif->getAnimation());
fclose($fp);*/
    }
    
    function upload_directory_image(){
       
        $param_array = $this->uri->uri_to_assoc(1);
        $upload = $param_array['upload']; 
        $network = $param_array['network'];
       
        if(!empty($param_array['file'])){
            $file = $param_array['file'];    
        }else{
            $file = "fileToUpload";
            if (!isset($_FILES['fileToUpload'])){
                list($file,$value) = each($_FILES);
            }
        }
        switch($upload){
             case 'banners' :
                $path = "./uploads/banners/";
                $width = 180;
                $height = 50;
            break;
            case 'company' : 
                $path = "./uploads/directory/company_logo/";
                $width = 150;
                $height = 150;
            break;
            case 'directory_news' : 
                $path = "./uploads/directory/news/";
                $width = 160;
                $height = 160;
            break;
            case 'userprofile' : 
                $path = "./uploads/directory/userprofile/";
                $width = 115;
                $height = 115;
            break;
            case 'photo' : 
                $path = "./uploads/directory/photo/";
                $width = 115;
                $height = 115;
            break;
            case 'network_logo' : 
                $path = "./uploads/network/";
                $width = 240;
                $height = 140;
            break;
            case 'crime_report' : 
                $path = "./uploads/crime_report/";
                $width = 115;
                $height = 115;
            break;
            case 'network_alerts' : 
                $path = "./uploads/alerts/";
                $width = 75;
                $height = 75;
            break;
            case 'landing_ads' : 
                $path = "./uploads/landing_ads/";
                $width = 385;
                $height = 320;
            break;
			case 'news_photo' :
				$path = "./uploads/news_photo/";		//if upload is not working please remove static from the path
                //$width = 550;
                //$height = 410;
			break;
				case "political_party":
                $path = "./uploads/political_party/thumbnail/";
                $width = 77;
                $height = 52;
            break;
            case 'profile' : 
                $path = "./uploads/profile/";
                $width = 80;
                $height = 80;
            break;
            case 'bug' : 
                $path = "./uploads/bug/";
                $width = 250;
                $height = 210;
            break;
            case 'donation' : 
                $path = "./uploads/donation/";
                $width = 250;
                $height = 210;
            break;
        }          
        
        $error = "";
        $msg = "";        
        $config['upload_path'] = $path;
		$config['maintain_ratio']   =  TRUE;
        $config['allowed_types'] = 'gif|jpg|png|jpeg';
        $config['max_size'] = '4000';
        $config['max_width'] = '4000';
        $config['max_height'] = '4000';
        $config['encrypt_name'] = false;
        $this->load->library('upload', $config);
        $this->upload->display_errors('','');

        if ( ! $this->upload->do_upload($file)){
            $error = $this->upload->display_errors();
         } else {
            $data = $this->upload->data();
            $original_image = $data['file_name'];
            $this->load->library('image_lib');
            if(!empty($network)){
                $new_name = $network.'-'.$data['file_name'];    
            }else{
                $new_name = $data['file_name'];    
            }
			
			 switch($upload){                				
				case "news_photo":
                    $split = 1;
					if(($data['image_width'] <= 550)){
                        $width = $data['image_width'];
                        $height = $data['image_height'];    
                    }else{
                        $width = 550;    
						$height = 520;
                    }
					$newsplit = 3;
				break;
                case 'photo' :
                    $split = 4;
                    $crop_width = 75;
                    $crop_height = 75;
                break;
                case 'userprofile' :
                    $split = 4;
                    $crop_width = 75;
                    $crop_height = 75;
                break;
                case 'profile' :                   
                    $split = 3;                    
                break;
                case 'crime_report' :                    
                    $split = 4;
                    $crop_width = 69;
                    $crop_height = 69;
                break;
                case 'directory_news' :                    
                    $split = 4;
                    $crop_width = 70;
                    $crop_height = 70;
                break;
                case 'network_logo' :                    
                    $split = 2;                    
                break;
            }
            
            if($upload != "political_party")
            {
                $new_name = str_replace(" ", "-", $new_name);                      
                $config['image_library']    = 'GD2';
                $config['source_image']     = $path . $data['file_name'];
                $config['maintain_ratio']   =  TRUE;
                $config['width']            = $width;            
                $config['height']           = $height;                        
                $config['new_image']        = $path.'/thumb/'.$new_name;
                $config['quality']          = '100%' ;        
                $this->image_lib->initialize($config);
    
                if ( ! $this->image_lib->resize())
                {
                    return $this->image_lib->display_errors('<div class="message error"><p>', '</p></div>');
                }
    
            }            
            switch($split){
                case '1' : // for news photo
                    $new_name = str_replace(" ", "-", $new_name); 
                           
                    $config['image_library']    = 'GD2';
                    $config['source_image']     = $path . $data['file_name'];
                    $config['maintain_ratio']   =  TRUE;
                    $config['width']            = 62;
                    $config['height']           = 62;
                    $config['new_image']        = $path.'/fake/'.$new_name;
                    $config['quality']          = '100%' ;        
                    $this->image_lib->initialize($config);

                    if ( ! $this->image_lib->resize())
                    {
                        return $this->image_lib->display_errors('<div class="message error"><p>', '</p></div>');
                    }
                break;
                case '2' : // not used                   
                    if(($data['image_width'] <= 550) && ($data['image_height'] <= 500)){
                        $image_width = $data['image_width'];
                        $image_height = $data['image_height'];    
                    }else{
                        $ratio = $data['image_width'] / $data['image_height'];
                        $image_height = $width * $ratio;
                        $image_width = 550;    
                    }
                    
                    $scale = 1;
					$final_width = 240; // wished output width for landscape perspective
					$final_height = 160; // wished output height landscape perspective
					
					// calculate the optimal size before we crop the overlapping part
					$x = $final_width/$image_width;
					$y = $final_height/$image_height;
					
					// get what is the best side to scale
					if($x < $y) {
						$new_width = round($image_width *($final_height/$image_height));
						$new_height = $final_height;
					}
					else {
						$new_height = round($image_height *($final_width/$image_width));
						$new_width = $final_width;
					}
					
					$x_axis = ($new_width - ($final_width *$scale))/2; // you have to divide it by 2 because you what to crop from the center
					$y_axis = ($new_height - ($final_height *$scale))/2; // you have to divide it by 2 because you what to crop from the center
                    
					// first step resize to the calculated size
					$config['image_library'] = 'GD2';
					$config['source_image'] = $path . $data['file_name'];;
					$config['maintain_ratio'] = true;
					$config['master_dim'] = 'width';
					$config['width'] = $new_width;
					$config['height'] = $new_height;
					$config['quality'] = '100%';
					$config['new_image'] = $path.'/thumb/'.$new_name;
					$this->image_lib->initialize($config);
					$this->load->library('image_lib', $config);
					$this->image_lib->resize();
					
					// now crop the image from the center
                    $config['image_library']    = 'GD2';                    
                    $config['source_image']     = $path.'/thumb/'.$new_name;
                    $config['maintain_ratio']   =  FALSE;
                    $config['x_axis']           = $x_axis;
                    $config['y_axis']           = $y_axis;                    
                    $config['width']            = $final_width;
                    $config['height']           = $final_height;
                    $config['new_image']        = $path.'/crop/'.$new_name;                    
                    $this->image_lib->initialize($config);

                    if ( ! $this->image_lib->crop())
                    {
                        echo $this->image_lib->display_errors();
                    }                	   
                break;
                case '3' : // profile picture

            		$image_width = $data['image_width'];
					$image_height = $data['image_height'];
										
					$scale = 1;
					$final_width = 52; // wished output width for landscape perspective
					$final_height = 52; // wished output height landscape perspective
					
					// calculate the optimal size before we crop the overlapping part
					$x = $final_width/$image_width;
					$y = $final_height/$image_height;
					
					// get what is the best side to scale
					if($x < $y) {
						$new_width = round($image_width *($final_height/$image_height));
						$new_height = $final_height;
					}
					else {
						$new_height = round($image_height *($final_width/$image_width));
						$new_width = $final_width;
					}
					
					$x_axis = ($new_width - ($final_width *$scale))/2; // you have to divide it by 2 because you what to crop from the center
					$y_axis = ($new_height - ($final_height *$scale))/2; // you have to divide it by 2 because you what to crop from the center
                    
					// first step resize to the calculated size
					$config['image_library'] = 'GD2';
					$config['source_image'] = $path . $data['file_name'];;
					$config['maintain_ratio'] = true;
					$config['master_dim'] = 'width';
					$config['width'] = $new_width;
					$config['height'] = $new_height;
					$config['quality'] = '100%';
					$config['new_image']        = $path.'/thumb/'.$new_name;
					$this->image_lib->initialize($config);
					$this->load->library('image_lib', $config);
					$this->image_lib->resize();
					
					// now crop the image from the center
                    $config['image_library']    = 'GD2';
                    //$config['source_image']     = $path . $data['file_name'];
                    //$config['source_image']     = $path.'/thumb/'.$new_name;
                    $config['source_image']     = $path.'/thumb/'.$new_name;
                    $config['maintain_ratio']   =  FALSE;
                    $config['x_axis']           = $x_axis;
                    $config['y_axis']           = $y_axis;
                    //$config['width']            = $crop_width;//75;
                    //$config['height']           = $crop_height;//75;
                    $config['width']            = $final_width;//75;
                    $config['height']           = $final_height;//75;
                    $config['new_image']        = $path.'/crop/'.$new_name;
                    //$config['quality']          = '100%';
                    $this->image_lib->initialize($config);

                    if ( ! $this->image_lib->crop())
                    {
                        echo $this->image_lib->display_errors();
                    }
                	/*
                    $config['image_library']    = 'GD2';
                    $config['source_image']     = $path.'/thumb/'.$new_name;
                    $config['maintain_ratio']   =  FALSE;
                    $config['x_axis']           = 0;
                    $config['y_axis']           = 0;
                    $config['width']            = 52;
                    $config['height']           = 52;
                    $config['new_image']        = $path.'/crop/'.$new_name;
                    //$config['quality']          = '100%';
                    $this->image_lib->initialize($config);

                    if ( ! $this->image_lib->crop())
                    {
                        echo $this->image_lib->display_errors();
                    }*/    
                break;
                case '4' : // directory photo
                    if(($data['image_width'] <= 550) && ($data['image_height'] <= 500)){
                        $prev_width = $data['image_width'];
                        $prev_height = $data['image_height'];                        
                    }else{
                        $ratio = $data['image_width'] / $data['image_height'];
                        $prev_width = 550;
                        $prev_height = 500;                        
                    }
                    
                    if($data['image_width'] > $data['image_height']){
                    	$x_axis = 33;
                    	$y_axis = 10;                    		
                    }
                    
            		if($data['image_width'] < $data['image_height']){
                    	$y_axis = 17;
                    	$x_axis = 0;                    		
                    }                    
                    
                    $new_name = str_replace(" ", "-", $new_name); 
                           
                    $config['image_library']    = 'GD2';
                    $config['source_image']     = $path . $data['file_name'];
                    $config['maintain_ratio']   =  TRUE;
                    $config['width']            = $prev_width;
                    $config['height']           = $prev_height;
                    $config['new_image']        = $path.'/preview/'.$new_name;
                    $config['quality']          = '100%' ;        
                    $this->image_lib->initialize($config);

                    if ( ! $this->image_lib->resize())
                    {
                        return $this->image_lib->display_errors('<div class="message error"><p>', '</p></div>');
                    }

                    $image_width = $data['image_width'];
					$image_height = $data['image_height'];
										
					$scale = 1;
					$final_width = 75; // wished output width for landscape perspective
					$final_height = 75; // wished output height landscape perspective
					
					// calculate the optimal size before we crop the overlapping part
					$x = $final_width/$image_width;
					$y = $final_height/$image_height;
					
					// get what is the best side to scale
					if($x < $y) {
						$new_width = round($image_width *($final_height/$image_height));
						$new_height = $final_height;
					}
					else {
						$new_height = round($image_height *($final_width/$image_width));
						$new_width = $final_width;
					}
					
					$x_axis = ($new_width - ($final_width *$scale))/2; // you have to divide it by 2 because you what to crop from the center
					$y_axis = ($new_height - ($final_height *$scale))/2; // you have to divide it by 2 because you what to crop from the center
                    
					// first step resize to the calculated size
					$config['image_library'] = 'GD2';
					$config['source_image'] = $path . $data['file_name'];;
					$config['maintain_ratio'] = true;
					$config['master_dim'] = 'width';
					$config['width'] = $new_width;
					$config['height'] = $new_height;
					$config['quality'] = '100%';
					$config['new_image']        = $path.'/thumb/'.$new_name;
					$this->image_lib->initialize($config);
					$this->load->library('image_lib', $config);
					$this->image_lib->resize();
					
					// now crop the image from the center
                    $config['image_library']    = 'GD2';
                    //$config['source_image']     = $path . $data['file_name'];
                    //$config['source_image']     = $path.'/thumb/'.$new_name;
                    $config['source_image']     = $path.'/thumb/'.$new_name;
                    $config['maintain_ratio']   =  FALSE;
                    $config['x_axis']           = $x_axis;
                    $config['y_axis']           = $y_axis;
                    //$config['width']            = $crop_width;//75;
                    //$config['height']           = $crop_height;//75;
                    $config['width']            = $final_width;//75;
                    $config['height']           = $final_height;//75;
                    $config['new_image']        = $path.'/crop/'.$new_name;
                    //$config['quality']          = '100%';
                    $this->image_lib->initialize($config);

                    if ( ! $this->image_lib->crop())
                    {
                        echo $this->image_lib->display_errors();
                    }    
                break;    
            }           
          
		 switch ($newsplit)
		{
			case 3:
					$src_image = $path . $data['file_name'];
					$thumb_size = 52;
					$image = getimagesize($path . $data['file_name']);
														
	                $image['format'] = strtolower(preg_replace('/^.*?\//', '', $image['mime']));
				
					// Import image
					switch( $image['format'] ) {
						case 'jpg':
						case 'jpeg':
							$image_data = imagecreatefromjpeg($src_image);
						break;
						case 'png':
							$image_data = imagecreatefrompng($src_image);
						break;
						case 'gif':
							$image_data = imagecreatefromgif($src_image);
						break;
						default:
							// Unsupported format
							return false;
						break;
					}
					
					 // Verify import
				    if( $image_data == false ) return false;
					
					  // Calculate measurements
					if( $image[0] > $image[1] ) {
						// For landscape images
						$x_offset = ($image[0] - $image[1]) / 2;
						$y_offset = 0;
						$square_size = $image[0] - ($x_offset * 2);
					} else {
						// For portrait and square images
						$x_offset = 0;
						$y_offset = ($image[1] - $image[0]) / 2;
						$square_size = $image[1] - ($y_offset * 2);
					}
					
					$canvas = imagecreatetruecolor(52, 52);
					
					    if( imagecopyresampled(
						$canvas,
						$image_data,
						0,
						0,
						$x_offset,
						$y_offset,
						$thumb_size,
						$thumb_size,
						$square_size,
						$square_size
					)) {
					 
					     // Create thumbnail
						 $dest_image = $path.'/crop/'.$new_name;
					switch( strtolower(preg_replace('/^.*\./', '', $dest_image)) ) {
						case 'jpg':
						case 'jpeg':
							imagejpeg($canvas, $dest_image, 200);
						break;
						case 'png':
							imagepng($canvas, $dest_image);
						break;
						case 'gif':
							imagegif($canvas, $dest_image);
						break;
						
					}
					 //echo $this->image_lib->display_errors()
			break;
		}
		
		}
			 
            $msg = $new_name;
            
        }
       
	
        switch($upload){
            case 'photo' : 
                echo "{ error: '" . $error . "',\n msg: '" . $msg . "',\n ori: '". $original_image ."' }";
            break;
            default:
                echo "{ error: '" . $error . "',\n msg: '" . $msg . "'\n }";    
        }
        /*
        echo "{ error: '" . $error . "',\n msg: '" . $msg . "'\n, ratio: '" . $ratio . "' }";
        echo "{";
        echo "error: '" . $error . "',\n";
        echo "msg: '" . $msg . "',\n";
        //echo "param: '" . print_r($_FILES) ."'\n";
        echo "}";*/
    
    }
	
	function uploadAttachment($networkId){
        $upload = $param_array['upload'];         
        if(!empty($param_array['file'])){
            $file = $param_array['file'];    
        }else{
            $file = "fileToUpload";
            if (!isset($_FILES['fileToUpload'])){
                list($file,$value) = each($_FILES);
            }
        }
		
		$config['upload_path'] = './uploads/message/'; 
        $config['allowed_types'] = 'doc|docx|rtf|pdf|txt|gif|jpg|png|jpeg';
        $config['max_size'] = '4000';
        $config['max_width'] = '4000';
        $config['max_height'] = '4000';
        $config['encrypt_name'] = true;
        $this->load->library('upload', $config);
        $this->upload->display_errors('','');

        if ( ! $this->upload->do_upload($file)){
            $error = $this->upload->display_errors();
         } else {
            $data = $this->upload->data();
            $original_image = $data['file_name'];
		}
		$msg = $data['file_name'];
		echo "{ error: '" . $error . "',\n msg: '" . $msg . "'\n }";  
	}
}
?>